const express = require('express')
const fs = require('fs');
const path = require('path')
const app = express()
const port = 3000
const router = require('./router/index')
const routerList = require('./router/list')
const FileStreamRotator = require('file-stream-rotator')
const morgan  = require('morgan');
app.use(express.json())// application/json
app.use(express.urlencoded())// x-www-from-urlencoded

//单个文件日志
// const accessLogStream = fs.createWriteStream(path.join(__dirname, 'db','log'), { flags: 'a' });
// app.use(morgan('short', {stream: accessLogStream}));

//日志拆分
const logDirectory = path.join(__dirname, 'log')
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
const accessLogStream = FileStreamRotator.getStream({
  date_format: 'YYYYMMDD',
  filename: path.join(logDirectory, 'access-%DATE%.log'),
  frequency: 'daily',
  verbose: false
})
app.use(morgan('combined', {stream: accessLogStream}))


//检错错误
app.use((err,req,res,next) => {
    res.status(500).send('错误了');
})
app.use('/api', router)
app.use('/api', routerList)
//未找到接口
app.use(function(req, res, next) {
    res.status(404).json({ error: '没有找到接口' })
});
app.listen(port, () => {
    console.log('3000端口运行中')
})